<template>
    <div class="magic_box">
      <div class="magic_top">
          <div class="magic_title">{{ t('magicBox') }}</div>
          <div class="magic_id">{{ '#' + recordItem.recordId }}</div>
      </div>
      <div class="magic_center">
        <div class="magic_amount">
          {{ recordItem.usdt }} USDT 
          <van-button class="magic_btn" v-if="status=='2'"  :disabled="recordItem.isClaminInsurance||recordItem.isClaimProfit|| store.state.isShowBtn" :loading="loading3" @click="batchClaim(3)" type="primary"> {{t('unclaimed')}}</van-button>
          <div v-else  class="magic_status">{{ status==1?t('underway'):t('completed')}}</div>
        </div>
        <div class="magic_item">
          <div class="magic_name">{{ t('magicStartTime') }}</div>
          <div class="magic_value">{{  recordItem.time }}</div>
        </div>
        <div class="magic_item">
          <div class="magic_name" v-if="status=='3'"> {{ t('received') }} </div>
          <div class="magic_name" v-else-if="status=='2'">{{ t('expectedSeedReturn') }}</div>
          <div class="magic_name" v-else-if="status=='1'"> {{ t('expectedSeedReturn') }}</div>


          <div class="magic_value" v-if="status=='3'"> 
            <span v-if="recordItem.nezhaRatio==0">
            {{ 
             parseFloat((recordItem.usdt*(valueStatus)).toFixed(4)) }}U (
              <span v-if="valueStatus==1.1">
                {{t('principal')}}  +10%)
              </span>
              <span v-if="valueStatus==1">
                {{t('principal')}} )
              </span>
              <span v-if="valueStatus==returnRate">
                {{(returnRate*100).toFixed(0)}}%{{ t('principal') }})
                <div v-if="computerPower"> {{  computerPower }}{{ t('computingPower') }}</div>
              </span>
            </span>
            <span v-else>

           {{ parseFloat((recordItem.usdt*(returnRate)).toFixed(4)) + 'U' }}   ({{(returnRate*100).toFixed(0)}}%{{ t('principal') }})
           <div v-if="computerPower"> 
            {{  parseFloat(((recordItem.usdt*(valueStatus-returnRate)*recordItem.nezhaRatio)).toFixed(2)) }} 
            积分 </div>

            </span>

           </div>

          <div class="magic_value" v-else> 
            
            
            {{ 
          
             parseFloat((recordItem.usdt*(valueStatus)).toFixed(4)) }}U (
              <span v-if="valueStatus==1.1">
                {{t('principal')}}  +10%)
              </span>
              <span v-if="valueStatus==1">
                {{t('principal')}} )
              </span>
              <span v-if="valueStatus==returnRate">
                {{(returnRate*100).toFixed(0)}}%{{ t('principal') }})
                <div v-if="computerPower"> {{  computerPower }}{{ t('computingPower') }}</div>
              </span>
           </div>
        </div>
        <div v-if="((recordItem.isClaminInsurance!==recordItem.isClaimProfit) || store.state.isShowBtn)&&valueStatus!=returnRate">
          <div>
            <van-button class="magic_btn" v-if="status=='2'" :disabled="recordItem.isClaminInsurance"  :loading="loading" @click="batchClaim(1)" type="primary"> {{ t('receiveTheInsuranceMoney') +'(65%)'}}</van-button>
          </div>
          <div class="flex_class" style="gap: 5px;;" > 
          <van-button class="magic_btn" v-if="status=='2'" :disabled="recordItem.isClaimProfit" :loading="loading5" @click="batchClaim(5)" type="primary">  {{t('claimScore')}} <span v-if="recordItem.isClaimProfit&&recordItem.nezhaRatio!=0">（{{t('received')}}）</span> </van-button>
          <span v-if="status=='2'">或</span>
          <van-button class="magic_btn" v-if="status=='2'" :disabled="recordItem.isClaimProfit" :loading="loading2" @click="batchClaim(2)" type="primary"> {{t('claimTheEarnings')}} {{ ((valueStatus-returnRate)*100).toFixed(0) + '%'}} <span v-if="recordItem.isClaimProfit&&recordItem.nezhaRatio==0">({{t('received')}})</span></van-button>
        </div>
        </div>
      
        <!-- <van-button class="magic_btn" type="primary"> 领取</van-button> -->
      </div>
      
    </div>
</template>
<script setup>
import { showToast } from "vant";
import { useStore } from "vuex";
import { ref, reactive, onMounted, computed } from "vue";
import IndexDBCache from '@/hooks/sql/useOrder.js'
const orderDB = new IndexDBCache()
import { useI18n } from "vue-i18n";
import sxbEth from "@/utils/sxbEth.js";
const { locale, t } = useI18n();
import {returnRate} from "@/utils/config.js"
import { useRouter } from "vue-router";
const router = useRouter();
const store = useStore();
const emit = defineEmits(["refresh"]);
const props = defineProps({
  title: {
    type: [String, Number],
    default: '',
  },
  value: {
    type: [String, Number],
    default: '',
  },
  valueStatus:{
    type:[String, Number],
    default:1.1
  },
  periodValue:{
    type:[String, Number],
    default:1
  },
  wheelData:{
    type:[Object],
    default:()=>{
      return {}
    }
  },
  recordItem:{
    type:[Object],
    default:()=>{
      return {}
    }
  }
})
const computerPower = computed(()=>{
  if(props.wheelData.margincallUsdt){
    return parseFloat((props.recordItem.usdt*props.wheelData.needVollar/props.wheelData.margincallUsdt).toFixed(4)) 
  }else{
    return 0
  }
  
})
const loading = ref(false)
const loading2 = ref(false)
const loading3 = ref(false)
const loading5 = ref(false)
const status = computed(()=> {
  if(store.state.currentWheel==props.wheelData.value){
    if( props.periodValue <=  store.state.currentPeriod - 5 ){
      if(props.recordItem.isClaminInsurance && props.recordItem.isClaimProfit){
        // "已完成"
        return  3
      }else{
        // "待领取"
        return  2 
      }
      
    }else{
      // return '进行中'
      return  1
    }
  }else{
    if(props.recordItem.isClaminInsurance && props.recordItem.isClaimProfit){
        return 3
      }else{
        return 2
      }
 
  }
})
// 批量领取
const batchClaim = async(value)=>{
  await orderDB.initDB()
  
  if(value==1){
    loading.value = true
  }else if(value==2){
    loading2.value = true
  }else if(value==3){
    loading3.value = true
  }else if(value==5){
    loading5.value = true
  }
  try {
    let hex
    if(value==1){
      hex = await  sxbEth.ContractWithSinger.batchClaimPrincipal([props.recordItem.recordId])
    }else if(value==2){
      hex = await  sxbEth.ContractWithSinger.batchClaimProfit([props.recordItem.recordId],false)
    }else if(value==3){
      hex = await  sxbEth.ContractWithSinger.batchClaim([props.recordItem.recordId])
    }else if(value==5){
      hex = await  sxbEth.ContractWithSinger.batchClaimProfit([props.recordItem.recordId],true)
    }
   await hex.wait();
   loading.value = false;
   showToast(t('claimSuccess'))
   let newObj = {...props.recordItem}
   if(value==1||value==3){
    newObj.isClaminInsurance = true
   }
  if(value==2||value==3||value==5){
    newObj.isClaimProfit = true
   }
   if(value==5){
    newObj.nezhaRatio = 2
   }
   newObj.isClaim = (newObj.isClaminInsurance && newObj.isClaimProfit)?1:2
   await orderDB.updateData('recordList', newObj)
   emit("refresh")
  } catch (error) {
    let errorStr = JSON.stringify(error)
    if(errorStr&&errorStr.indexOf("Insufficient interest balance")>=0){
      showToast(t('congratulationsOnBigBonus'))
      store.commit("changeShowBtn",true)
    }else{
      if(store.state.isShowError){
        showToast(t('claimFail')+errorStr)
      }else{
        showToast(t('claimFail'))
      }
    }
    loading.value = false;
  }finally{
    if(value==1){
      loading.value = false
    }else if(value==2){
      loading2.value = false
    }else if(value==3){
      loading3.value = false
    }else if(value==5){
      loading5.value = false
    }
  }
}
const goToPage = (value) => {
  router.push({
    name: value
  })
}
</script>
<style lang="scss" scoped>
.magic_box{
  background: #121b22;
  border-radius: 10px;
  margin-top: 14px;
  .magic_btn{
    margin-top: 6px;
    height: 34px;
  }
  .magic_top{
    height: 40px;
    border-radius: 10px 10px 0 0;
    padding: 10px;
    background: #4d1119;
    .magic_title{
      font-size: 16px;
      text-align: right;
    }
    .magic_id{
      text-align: right;
    }
  }
  .magic_center{
    padding: 10px 14px;
    .magic_amount{
      height: 40px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .magic_btn{
        height: 26px;
        width: auto;
        border-radius: 30px;
      }
      .magic_status{
        border-radius: 30px;
        color: #f50106;
        border: 1px solid #f50106;
        padding: 4px 10px;
      }
    }
    .magic_item{
      position: relative;
      display: flex;
      align-items: center;
      justify-content: space-between;
      color: #999;
      padding: 10px;
      &::before{
        content: " ";
        position: absolute;
        left: 0;
        top: 50%;
        height: 5px;
        width: 5px;
        border-radius: 50%;
        background: #f50106;
      }
    }
  }
}

</style>
  